package com.dalei.vdsmanager.management.vds.service;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import com.dalei.vdsmanager.management.vds.dao.VDSUser;
import com.dalei.vdsmanager.management.vds.dao.VDSUserDao;
import com.dalei.vdsmanager.management.vds.security.MD5Encryption;

public class VDSUserServiceImpl implements VDSUserService, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6736338963622004892L;

	private VDSUserDao vdsUserDao;

	public VDSUserDao getVdsUserDao() {
		return vdsUserDao;
	}

	public void setVdsUserDao(VDSUserDao vdsUserDao) {
		this.vdsUserDao = vdsUserDao;
	}

	@Override
	public void addVDSUser(VDSUser vdsUser) {
		// TODO Auto-generated method stub
		if (vdsUser != null) {
			String password = vdsUser.getPassword();
			String passwordMD5 = null;
			try {
				passwordMD5 = MD5Encryption.getEncryptedPassword(password);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			vdsUser.setPassword(passwordMD5);
			vdsUserDao.saveVDSUser(vdsUser);
		}
	}

	@Override
	public boolean checkRepeat(String userName) {
		// TODO Auto-generated method stub
		List<VDSUser> vdsUsers = getVDSUserList();
		for (VDSUser user : vdsUsers) {
			if (user.getUser().equals(userName)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<VDSUser> getVDSUserList() {
		// TODO Auto-generated method stub
		return vdsUserDao.findVDSUsers();
	}

	@Override
	public VDSUser getVDSUserById(int id) {
		// TODO Auto-generated method stub
		return vdsUserDao.findVDSUserById(id);
	}

	@Override
	public VDSUser getVDSUserByUserName(String userName) {
		// TODO Auto-generated method stub
		return vdsUserDao.findVDSUserByUserName(userName);
	}

	@Override
	public boolean changePassword(int id, String newPasssword) {
		// TODO Auto-generated method stub
		VDSUser user = getVDSUserById(id);
		if (user != null) {
			if (newPasssword != null) {
				String encryptedPassword = null;
				try {
					encryptedPassword = MD5Encryption
							.getEncryptedPassword(newPasssword);
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return false;
				}
				user.setPassword(encryptedPassword);
				vdsUserDao.updateVDSUser(user);
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	public void removeVDSUser(int id) {
		// TODO Auto-generated method stub
		VDSUser vdsUser = getVDSUserById(id);
		if (vdsUser != null) {
			vdsUserDao.deleteVDSUser(vdsUser);
		}
	}
}
